//
// Created by liangfuqiang on 2021/12/4.
//

#include <stdlib.h>
#include <stdio.h>

typedef struct Vector {
    int *data;
    int size, capacity;
} Vector;

Vector *init(int size) {
    Vector *vector = (Vector *) malloc(sizeof(Vector));
    vector->data = (int *) malloc(sizeof(int) * size);
    vector->size = 0;
    vector->capacity = size;

    return vector;
}

void delete(Vector *vector) {
    free(vector->data);
    free(vector);
}

int insert(Vector *vector, int index, int val) {
    if (index > vector->size || index < 0) {
        return -1;
    }
    if (vector->capacity == vector->size) {
        vector->capacity *= 2;
        vector->data = (int *) realloc(vector->data, sizeof(int) * vector->capacity);
    }
    for (int i = vector->size; i > index; --i) {
        vector->data[i] = vector->data[i - 1];
    }
    vector->data[index] = val;
    vector->size += 1;
    return 0;
}

int del_elem(Vector *vector, int index) {
    if (vector->size < 1 || vector->size <= index || index < 0) {
        return -1;
    }
    for (int i = index; i < vector->size; ++i) {
        vector->data[i - 1] = vector->data[i];
    }
    vector->size--;
    return 0;
}

void print(Vector *vector) {
    printf("size = %d, capacity = %d, elements = [ ", vector->size, vector->capacity);
    for (int i = 0; i < vector->size; ++i) {
        printf("%d ", vector->data[i]);
    }
    printf("]\n");
}